<?php
/**
 * @author zhangxin
 * @desc 微信支付封装
 */
$qrcodeLibPath = dirname(__FILE__) . '/../library/ThirdParty/Qrcode/';
include_once $qrcodeLibPath . 'Qrcode.php';

class WxpayController extends Yaf_Controller_Abstract
{
    public function createbillAction()
    {
        $itemid = $this->getRequest->getQuery("itemid", "");
        if (!$itemid) {
            echo Common_Request::Response(-6001, Err_Map::get(-6001));
            return false;
        }

        /**
         * 登陆校验
         */
        session_start();
        if (!isset($_SESSION['user_token_time']) || !isset($_SESSION['user_token']) || !isset($_SESSION['user_id'])
            || md5("salt" . $_SESSION['user_token_time'] . $_SESSION['user_id']) != $_SESSION['user_token']) {
            echo json_encode(array("errno" => -6002, "errmsg" => "请先登录后操作"));
            return false;
        }

        //创建订单
        $model = new WxpayModel();
        if ($data = $model->createbill($itemid, $_SESSION['user_id'])) {
            echo Common_Request::Response(0, "", $data);
        } else {
            echo Common_Request::Response([
                "errno"  => $model->errno,
                "errmsg" => $model->errmsg,
            ]);
        }
        return true;
    }

    public function qrcodeAction()
    {
        $billId = $this->getRequest()->getQuery("billId", "");
        if (!$billId) {
            echo Common_Request::Response(-6008, Err_Map::get(-6008));
            return false;
        }

        //二维码生成
        $model = new WxpayModel();
        if ($data = $model->qrcode($billId)) {
            /**
             * 二维码输出
             */
            QRcode::png($data);
        } else {
            echo Common_Request::Response([
                "errno"  => $model->errno,
                "errmsg" => $model->errmsg,
            ]);
        }
        return true;
    }

    public function callbackAction()
    {
        $model = new WxpayModel();
        $model->callback();
        echo Common_Request::Response(0, Err_Map::get(0));
        return true;
    }
}

}

}
